package com.vitalfit.service.serviceImpl;

import com.vitalfit.model.dto.FoodItemDTO;
import com.vitalfit.model.entity.food.FoodCategory;
import com.vitalfit.model.entity.food.FoodItem;
import com.vitalfit.model.vo.FoodItemPageVO;
import com.vitalfit.repository.FoodCategoryRepository;
import com.vitalfit.repository.FoodItemRepository;
import com.vitalfit.service.FoodItemService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
@Slf4j
public class FoodItemServiceImpl implements FoodItemService {

    private final FoodCategoryRepository foodCategoryRepository;
    private final FoodItemRepository foodItemRepository;

    @Override
    @Transactional
    public FoodItem addFoodItem(FoodItemDTO foodItemDTO) {
        List<FoodCategory> categories = foodCategoryRepository.findAllById(foodItemDTO.getCategoryIds());
        if (categories.size() != foodItemDTO.getCategoryIds().size()) {
            throw new RuntimeException("Some categories not found");
        }

        // 设置类别
        FoodItem foodItem = new FoodItem();

        foodItem.setImageUrl(foodItemDTO.getImageUrl());
        foodItem.setFoodCategories(categories);
        foodItem.setBasePrice(foodItemDTO.getBasePrice());
        foodItem.setDescription(foodItemDTO.getDescription());
        foodItem.setName(foodItemDTO.getName());

        categories.forEach(foodCategory -> {
            int currentCount = foodCategory.getDishCount();
            foodCategory.setDishCount(currentCount + 1);
            foodCategoryRepository.save(foodCategory);
        });

        // 保存食品项
        return foodItemRepository.save(foodItem);
    }

    @Override
    @Transactional
    public FoodItem updateFoodItem(Integer foodId, FoodItemDTO foodItemDTO) {
        FoodItem foodItem = foodItemRepository.findById(foodId).orElseThrow(() -> new RuntimeException("FoodItem not found"));

        foodItem.setName(foodItemDTO.getName());
        foodItem.setDescription(foodItemDTO.getDescription());
        foodItem.setBasePrice(foodItemDTO.getBasePrice());

        // 更新类别关系
        List<FoodCategory> categories = foodCategoryRepository.findAllById(foodItemDTO.getCategoryIds());
        foodItem.setFoodCategories(categories);

        return foodItemRepository.save(foodItem);

    }

    @Override
    @Transactional(readOnly = true)
    public FoodItemPageVO listAllFoodItems(int page, int size) {
        Pageable pageable = PageRequest.of(page, size);
        FoodItemPageVO foodItemPageVO = new FoodItemPageVO();
        Page<FoodItem>  foodItemPage = foodItemRepository.findAll(pageable);
        foodItemPageVO.setTotalPages(foodItemPageVO.getTotalPages());
        foodItemPageVO.setTotalElements(foodItemPage.getTotalElements());
        foodItemPageVO.setContent(foodItemPage.getContent());
        return foodItemPageVO;
    }


    @Override
    @Transactional(readOnly = true)
    public FoodItem getFoodById(Integer foodId) {

        return foodItemRepository.findById(foodId).orElseThrow(
                ()-> new RuntimeException("FoodItem not found")
        );
    }




}
